package cn.bat.slsystem.control;



import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bat.slsystem.pojo.AuFunction;
import cn.bat.slsystem.pojo.QuanXianList;
import cn.bat.slsystem.service.AuAuthorityService;
import cn.bat.slsystem.service.AuFunctionServicce;
import cn.bat.slsystem.tools.Constants;

@Controller
@RequestMapping(value="/aufunction")
public class AuFunctionContorller {
	@Autowired
	private AuFunctionServicce auFunctionServicce;
	@Autowired
	private AuAuthorityService auAuthorityService;
	
	@RequestMapping(value="/serch.html")
	@ResponseBody
	public Object serch(int roleid){
		List<AuFunction> list=auFunctionServicce.serch(roleid);
		List<QuanXianList> newlist=new ArrayList<QuanXianList>();
		for (AuFunction auFunction : list) {
			int parentid=Integer.parseInt(auFunction.getId().toString());
			List<AuFunction> list1=auFunctionServicce.serchbyrole(roleid, parentid);
			String name=auFunction.getFunctionname();
			QuanXianList qxl=new QuanXianList(name,list1);
			newlist.add(qxl);
		}
		
		return newlist;
	}
	
	@RequestMapping("/quanxian.html")
	public String quanxian(HttpSession session){
		List<String> list = (List) session.getAttribute(Constants.lanjie);
		for (String path : list) {
			if(path.equals("aufunction/quanxian.html")){
				return "quanxian";
			}
		}
		return "login";
	}

	
	@RequestMapping(value="/serchid.html")
	@ResponseBody
	public Object serchid(int roleid){
		return auAuthorityService.serchid(roleid);
		
	}
	
	@RequestMapping(value="/change.html")
	@ResponseBody
	public String change(int roleid,int[] functionids){
		auAuthorityService.del(roleid);
		System.out.println(roleid);
		for (int functionid : functionids) {
			System.out.println(functionid);
			auAuthorityService.add(roleid, functionid);
		}
		
		return null;
	}

}
